﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using CPGM;

namespace CPGM_ReceiverTest
{
    public partial class frmReceiver : Form
    {
        Socket s;
        CPGMReceiver rec = new CPGMReceiver();

        public frmReceiver()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (rec.IsConnected)
            {
                rec.Disconnect();
                btnConnect.Text = "Connect";
                lstLog.Items.Add("Connection closed.");
            }
            else
            {
                rec.Connect(txtMulticastGroup.Text, txtPortNumber.Text);

                lstLog.Items.Add("Waiting for data...");

                rec.StartReceiving();

                btnConnect.Text = "Disconnect";
            }
        }

        private void rec_DataReceived(object sender, SocketPacketEventArgs spe)
        {
            string st = string.Format("RX:Received package {0} from {1}",spe.Packet.ID, spe.Packet.LocalIPAddress);
            Invoke(new MethodInvoker(delegate
            {
                lstLog.Items.Add(st);
                lstLog.Items.Add("Waiting for data...");
            }));
        }

        private void frmReceiver_Load(object sender, EventArgs e)
        {
            rec.DataReceived += new CPGMReceiver.OnDataReceived(rec_DataReceived);
        }
    }
}
